package com.plata.base.aclr.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class DeviceUtils {
    private static final String TAG = "DeviceUtils";

    private static final String PROPERTY_RELEASE_VERSION = "ro.release.version";
    private static final String FILE_NAME_MEMINFO = "/proc/meminfo";
    private static final String FORMAT_OS_VERSION = "Android %s";
    private static final String FORMAT_NUMBER = "%s*%s";

    private static long sTotalMemory;

    static {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME_MEMINFO)), 1000);
            char[] memoryInfo = reader.readLine().trim().toCharArray();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < memoryInfo.length; i++) {
                if (memoryInfo[i] <= '9' && memoryInfo[i] >= '0') {
                    buffer.append(memoryInfo[i]);
                }
            }
            sTotalMemory = Long.parseLong(buffer.toString());
        } catch (Exception e) {
            LogTool.w(TAG, "staticModule. " + e.toString());
            sTotalMemory = 1;
        } finally {
            IOUtils.closeSilently(reader);
        }
    }

    static String deviceId = null;

    @SuppressLint("MissingPermission")
    public static String getDeviceId(Context context) {
        try {
            if (!TextUtils.isEmpty(deviceId)) {
                return deviceId;
            }
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            deviceId = manager.getDeviceId();
            return deviceId;
        } catch (Exception e) {
            return "";
        }
    }

    static String androidId = null;

    public static String getAndroidId(Context context) {
        if (androidId != null) {
            return androidId;
        }
        String tmp;
        try {
            tmp = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
            if ("9774d56d682e549c".equals(tmp)) {
                tmp = null;
            }
        } catch (Throwable e) {
            tmp = null;
        }
        if (tmp == null) {
            androidId = "";
        } else {
            androidId = tmp;
        }
        return androidId;
    }
}
